#include <QCoreApplication>

#include <iostream>
#include <vector>
#include <fstream>
#include <string>


#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
# include <opencv2/calib3d/calib3d.hpp>
#include  "cameracalibrator.h"
#include <iomanip>


int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);

    std::ifstream inImgPath("./calibdata.txt");
    std::ofstream fout("caliberation_result.txt");
    std::cout << "Start to extract corners......"  << std::endl;
    int image_count = 0;
    cv::Size image_size;
    cv::Size board_size = cv::Size(7,11);
    cv::Mat imageInput;
    std::string filename;
    std::vector<std::string> filelist  ;

    if (!inImgPath)
    {
        std::cout << "Can't load files" << std::endl;
    }
    while (std::getline(inImgPath,filename))
    {
        filelist.push_back(filename);
    }

    while (image_count < filelist.size())
    {
        filename = filelist[image_count++];

        std::cout << "image_count = " << image_count << std::endl;
        std::cout << filename.c_str() << std::endl;

        imageInput = cv::imread(filename);

        if (image_count == 1)
        {
            image_size.width = imageInput.cols;
            image_size.height = imageInput.rows;
            std::cout << "image_size.width = " << image_size.width << std::endl;
            std::cout << "image_size.height = " << image_size.height << std::endl;
        }
    }

    CameraCalibrator calibr;
    calibr.addChessboardPoints(board_size, imageInput);
    std::cout << "000" << std::endl;
    cv::Mat result = calibr.remap(imageInput, image_size);
    std::cout << "111" << std::endl;
    cv::namedWindow("image");
    cv::imshow("image", imageInput);
    cv::namedWindow("result");
    cv::imshow("result", result);

    cv::waitKey(500);

    return a.exec();
}
